package com.gjs.common.manager;

import cn.hutool.core.util.StrUtil;
import com.gjs.common.enums.FileTypeEnum;
import com.gjs.common.pojo.dto.upload.UploadRuleDTO;

import javax.xml.bind.ValidationException;
import java.util.*;
import java.util.stream.Collectors;

public class FileValidation {

    /**
     * 文件校验规则
     */
    private static Map<FileTypeEnum, UploadRuleDTO> fileValidationRule = new HashMap<>();

    static {
        // 图片校验规则
        Map<String, String> picRule = new HashMap<>();
        picRule.put("png", "image/png");
        picRule.put("jpg", "image/jpeg");
        picRule.put("jpeg", "image/jpeg");
        fileValidationRule.put(FileTypeEnum.PIC, new UploadRuleDTO("图片", picRule, 1048576));//2097152

        // 表格/PDF校验规则
        Map<String, String> docRule = new HashMap<>();
        docRule.put("xls", "application/vnd.ms-excel");
        docRule.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        docRule.put("pdf", "application/pdf");
        fileValidationRule.put(FileTypeEnum.DOC, new UploadRuleDTO("表格/PDF", docRule, 10485760));

        // 视频校验规则
        Map<String, String> videoRule = new HashMap<>();
        videoRule.put("mp4", "video/mp4");
        videoRule.put("avi", "video/avi");
        videoRule.put("wmv", "video/x-ms-wmv");
        fileValidationRule.put(FileTypeEnum.VIDEO, new UploadRuleDTO("视频", videoRule, 10485760));

        // 音频校验规则
        Map<String, String> audioRule = new HashMap<>();
        audioRule.put("mp3", "audio/mpeg");
        fileValidationRule.put(FileTypeEnum.AUDIO, new UploadRuleDTO("音频", audioRule, 15728640));
    }

    /**
     * 根据Content-Type和文件大小，校验
     *
     * @param contentType        String 文件Content-Type
     * @param size               Integer 文件大小，单位：Byte
     * @param allowFileTypeRange FileTypeEnum 限定允许的文件类型范围，不传则默认全部的文件类型
     */
    public static void check(String contentType, Integer size, FileTypeEnum... allowFileTypeRange) throws ValidationException {
        Integer allowSize = 0;
        Set<FileTypeEnum> checkFileType = allowFileTypeRange.length == 0 ? fileValidationRule.keySet() : Arrays.stream(allowFileTypeRange).collect(Collectors.toSet());
        for (FileTypeEnum fileTypeEnum : checkFileType) {
            UploadRuleDTO ruleDTO = fileValidationRule.get(fileTypeEnum);
            Map<String, String> rule = ruleDTO.getExt();
            if(rule.values().contains(contentType)){
                allowSize = ruleDTO.getLimitSize();
            }
        }
        if (allowSize.equals(0)){
            throw new ValidationException("不允许该文件类型");
        }else{
            if(size > allowSize){
                throw new ValidationException(StrUtil.format("文件大小最大不得超过：{}M", (allowSize / 1024 / 1024)));
            }
        }
    }

    /**
     * 获取List形式的规则
     * @param range FileTypeEnum 需要返回的文件类型范围，不传则默认全部的文件类型
     * */
    public static List<UploadRuleDTO> toList(FileTypeEnum... range){
        List<UploadRuleDTO> ruleDTOList = new LinkedList<>();
        Set<FileTypeEnum> fileTypeEnumSet = range.length == 0 ? fileValidationRule.keySet() : Arrays.stream(range).collect(Collectors.toSet());
        for (FileTypeEnum fileTypeEnum : fileTypeEnumSet) {
            ruleDTOList.add(fileValidationRule.get(fileTypeEnum));
        }
        return ruleDTOList;
    }
}
